﻿using InkjetControl.NLog;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Input;

namespace InkjetControl.model
{
    public delegate void CloseEventHandler();
    public delegate void CancelEventHandler();
    public delegate void ConfirmEventHandler();

    public class MsgDialogInfoModel : ViewModelBase
    {
        private string _message;
        private Visibility _isShowCloseBtn;
        private Visibility _isShowCancelBtn;
        private Visibility _isShowConfirmBtn;

        public ICommand CloseBtnCommand { get; }
        public ICommand CancelBtnCommand { get; }
        public ICommand ConfirmBtnCommand { get; }


        public event CloseEventHandler OnDialogCloseBtnHandler;
        public event CancelEventHandler OnDialogCancelBtnHandler;
        public event ConfirmEventHandler OnDialogConfirmBtnHandler;

        public MsgDialogInfoModel()
        {
            CloseBtnCommand = new AnotherCommandImplementation(OnCloseBtnAction);
            CancelBtnCommand = new AnotherCommandImplementation(OnCancelBtnAction);
            ConfirmBtnCommand = new AnotherCommandImplementation(OnConfirmBtnAction);
            _isShowCloseBtn = Visibility.Visible;
            _isShowCancelBtn = Visibility.Hidden;
            _isShowConfirmBtn = Visibility.Hidden;
        }


        public string Msg
        {
            get => _message;
            set => SetProperty(ref _message, value);
        }

        public Visibility IsShowCloseBtn
        {
            get => _isShowCloseBtn;
            set => SetProperty(ref _isShowCloseBtn, value);
        }
        public Visibility IsShowCancelBtn
        {
            get => _isShowCancelBtn;
            set => SetProperty(ref _isShowCancelBtn, value);
        }
        public Visibility IsShowConfirmBtn
        {
            get => _isShowConfirmBtn;
            set => SetProperty(ref _isShowConfirmBtn, value);
        }


        private void OnCloseBtnAction(object obj)
        {
            OnDialogCloseBtnHandler?.Invoke();
            CloseDialog();
        }

        private void OnCancelBtnAction(object obj)
        {
            OnDialogCancelBtnHandler?.Invoke();
            CloseDialog();
        }

        private void OnConfirmBtnAction(object obj)
        {
            OnDialogConfirmBtnHandler?.Invoke();
            CloseDialog();
        }

        private void CloseDialog()
        {
            try
            {
                if (DialogHost.IsDialogOpen("RootDialog"))
                {
                    DialogHost.Close("RootDialog");
                }
            }
            catch (Exception e)
            {
                Log.Logger.Warn(e, "CloseDialog");
            }
        }
    }
}
